using System;
using System.Linq;
using Castle.MicroKernel.Registration;

namespace ThunderJob.Bootstrapper.Extensions
{
    public static class WindsorExtensions
    {

        public static BasedOnDescriptor FirstNonGenericCoreInterface(this ServiceDescriptor descriptor, string interfaceNamespace)
        {
            return descriptor.Select(delegate(Type type, Type baseType)
            {
                var interfaces = type.GetInterfaces()
                    .Where(t => t.IsGenericType == false && t.Namespace.StartsWith(interfaceNamespace));

                if (interfaces.Count() > 0)
                    return new[] {interfaces.ElementAt(0)};

                return null;
            });
        }
    }
}